package com.gy.api.pc.examine.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.gy.api.base.Constants;
import com.gy.api.base.Type;
import com.gy.api.mobile.company.dao.CompanyInfoDao;
import com.gy.api.mobile.company.domain.po.CompanyInfoPO;
import com.gy.api.pc.examine.dao.ExamineDao;
import com.gy.api.pc.examine.dao.ExamineLogDao;
import com.gy.api.pc.examine.domain.po.ExamineLogPO;
import com.gy.api.pc.examine.domain.po.ExaminePO;
import com.gy.api.pc.examine.domain.vo.*;
import com.gy.api.pc.examine.service.IExamineService;
import com.gy.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
@Slf4j
public class ExamineServiceImpl implements IExamineService {

    @Autowired
    ExamineLogDao examineLogDao;
    @Autowired
    ExamineDao examineDao;

    @Override
    public void addExamine(ExamineAddVO examineAddVO) {
        ExaminePO examinePO=new ExaminePO();
        BeanUtils.copyProperties(examineAddVO, examinePO);
        examinePO.preInsert();
        examinePO.setExamineState(1);
        examinePO.setCreateBy(examineAddVO.getCreateById());
        examinePO.setUpdateBy(examineAddVO.getCreateById());
//        this.addExamineLog(new ExamineLogAddVO(examineAddVO.getRelatedId(), Constants.EXAMINE_LOG_AGREE,Constants.EXAMINE_ONE_NODE,examineAddVO.getCreateByName()
//        ,examineAddVO.getCreateById(),"提交审核",null,null,examinePO.getId()));
        examineDao.insertSelective(examinePO);
    }

    @Override
    public void addExamineState(ExamineAddVO examineAddVO,Integer examineState) {
        ExaminePO examinePO=new ExaminePO();
        BeanUtils.copyProperties(examineAddVO, examinePO);
        examinePO.preInsert();
        examinePO.setExamineState(examineState);
        examinePO.setCreateBy(examineAddVO.getCreateById());
        examinePO.setUpdateBy(examineAddVO.getCreateById());
//        if(examineState!=0){
//            this.addExamineLog(new ExamineLogAddVO(examineAddVO.getRelatedId(), Constants.EXAMINE_LOG_AGREE,Constants.EXAMINE_ONE_NODE,examineAddVO.getCreateByName()
//                    ,examineAddVO.getCreateById(),"提交审核",null,null,examinePO.getId()));
//        }
        examineDao.insertSelective(examinePO);
    }

    @Override
    public void addExamineLog(ExamineLogAddVO examineLogAddVO) {
        ExamineLogPO examineLogPO=new ExamineLogPO();
        BeanUtils.copyProperties(examineLogAddVO, examineLogPO);
        examineLogPO.setExamineDate(new Date());
        examineLogPO.setId(IdUtil.getSnowflakeNextIdStr());
        examineLogDao.insertSelective(examineLogPO);
    }


    @Override
    public boolean checkExamine(String relatedId) {
        Example example=new Example(ExaminePO.class);
        example.and().andEqualTo("relatedId",relatedId).andEqualTo("examineState",1);
        List<ExaminePO> list=examineDao.selectByExample(example);
        if(list==null||list.size()==0){
            return false;
        }
        return true;
    }


    @Override
    public ExamineRecordVO getCount() {
        ExamineRecordVO examineRecordVO=new ExamineRecordVO();
        Example example=new Example(ExaminePO.class);
        example.and().andEqualTo("examineState","0").andEqualTo("createBy", StpUtil.getLoginIdAsString()).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setUncommittedNum(examineDao.selectCountByExample(example));

        example=new Example(ExaminePO.class);
        example.and().andEqualTo("examineState","1").andEqualTo("createBy", StpUtil.getLoginIdAsString()).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setHandleNum(examineDao.selectCountByExample(example));

        example=new Example(ExaminePO.class);
        List<Integer> list=new ArrayList<>();
        list.add(2);
        list.add(3);
        list.add(4);
        example.and().andEqualTo("createBy", StpUtil.getLoginIdAsString()).andIn("examineState",list).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setCompleteNum(examineDao.selectCountByExample(example));

        example=new Example(ExaminePO.class);
        example.and().andEqualTo("examineState","5").andEqualTo("createBy", StpUtil.getLoginIdAsString()).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setCancelNum(examineDao.selectCountByExample(example));

        return examineRecordVO;
    }

    @Override
    public ExamineCompletedRecordVO getCompletedCount() {

        ExamineCompletedRecordVO examineRecordVO=new ExamineCompletedRecordVO();
        Example example=new Example(ExaminePO.class);
        example.and().andEqualTo("examineState","2").andEqualTo("createBy", StpUtil.getLoginIdAsString()).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setAgreeNum(examineDao.selectCountByExample(example));

        example=new Example(ExaminePO.class);
        example.and().andEqualTo("examineState","3").andEqualTo("createBy", StpUtil.getLoginIdAsString()).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setDisagreeNum(examineDao.selectCountByExample(example));

        example=new Example(ExaminePO.class);
        example.and().andEqualTo("examineState","4").andEqualTo("createBy", StpUtil.getLoginIdAsString()).andNotEqualTo("examineType",Constants.EXAMINE_TYPE_ENTERPRISE_CERTIFICATION);
        examineRecordVO.setReturnNum(examineDao.selectCountByExample(example));

        return examineRecordVO;
    }

    @Override
    public List<HandleRecordVO> handleRecord(HandleRecordDto dto) {
        dto.setUserId(StpUtil.getLoginIdAsString());
        return examineDao.handleRecord(dto);
    }

    @Override
    public List<HandleRecordVO> handleRecordPage(HandleRecordPageDto dto) {
        dto.setUserId(StpUtil.getLoginIdAsString());
        return examineDao.handleRecordPage(dto);
    }

    @Override
    public List<ExamineLogVO> getNodesByFlowId(String flowId) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(flowId);
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"流程信息未找到！");
        }
        Example example1=new Example(ExamineLogPO.class);
        example1.and().andEqualTo("examineId",flowId);
        example1.setOrderByClause("examine_date desc");
        List<ExamineLogPO> examineLogPOS=examineLogDao.selectByExample(example1);
        List<ExamineLogVO> examineLogVOList=new ArrayList<>();
        for (ExamineLogPO examineLogPO:examineLogPOS){
            ExamineLogVO examineLogVO=new ExamineLogVO();
            BeanUtils.copyProperties(examineLogPO, examineLogVO);
            examineLogVOList.add(examineLogVO);
        }
       return examineLogVOList;

    }

    @Override
    public ExamineLogVO getNodeById(String taskId) {
        ExamineLogPO examineLogPO=examineLogDao.selectByPrimaryKey(taskId);
        if(examineLogPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        ExamineLogVO examineLogVO=new ExamineLogVO();
        BeanUtils.copyProperties(examineLogPO, examineLogVO);
        return examineLogVO;
    }


    @Autowired
    private CompanyInfoDao companyInfoDao;

    @Override
    public void withdraw(String businessId) {
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(companyInfoPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),"信息未找到");
        }
        ExaminePO examinePO=examineDao.selectByPrimaryKey(businessId);
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"流程信息未找到！");
        }

        if(examinePO.getExamineState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"当前状态不是审批中，不能撤销");
        }
        examinePO.setExamineState(5);
        examinePO.setExamineUserIds("");
        String EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_REVOKE;
        examinePO.preUpdate();
        examineDao.updateByPrimaryKey(examinePO);
        this.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), EXAMINE_LOG_AGREE,examinePO.getExamineNode(),companyInfoPO.getUserName()
                ,companyInfoPO.getId(),"撤销","","",examinePO.getId()));
    }
}